#! /bin/sh
# $OpenLDAP$
## This work is part of OpenLDAP Software <http://www.openldap.org/>.
##
## Copyright 1998-2022 The OpenLDAP Foundation.
## All rights reserved.
##
## Redistribution and use in source and binary forms, with or without
## modification, are permitted only as authorized by the OpenLDAP
## Public License.
##
## A copy of this license is available in the file LICENSE in the
## top-level directory of the distribution or, alternatively, at
## <http://www.OpenLDAP.org/license.html>.

echo "running defines.sh"
. $SRCDIR/scripts/defines.sh

mkdir -p $TESTDIR $CONF1.d $DBDIR1 $DBDIR2

$SLAPPASSWD -g -n >$CONFIGPWF
echo "rootpw `$SLAPPASSWD -T $CONFIGPWF`" >$TESTDIR/configpw.conf

if test $AC_lloadd = lloaddyes ; then
    echo "Load balancer module not available, skipping..."
    exit 0
fi

echo "Starting the first slapd on TCP/IP port $PORT2..."
. $CONFFILTER $BACKEND < $CONF > $CONF2
$SLAPADD -f $CONF2 -l $LDIFORDERED
RC=$?
if test $RC != 0 ; then
    echo "slapadd failed ($RC)!"
    exit $RC
fi

echo "Running slapindex to index slapd database..."
$SLAPINDEX -f $CONF2
RC=$?
if test $RC != 0 ; then
    echo "warning: slapindex failed ($RC)"
    echo "  assuming no indexing support"
fi

$SLAPD -f $CONF2 -h $URI2 -d $LVL > $LOG2 2>&1 &
PID=$!
if test $WAIT != 0 ; then
    echo PID $PID
    read foo
fi
PID2="$PID"
KILLPIDS="$PID"

echo "Testing slapd searching..."
for i in 0 1 2 3 4 5; do
    $LDAPSEARCH -s base -b "$MONITOR" -H $URI2 \
        '(objectclass=*)' > /dev/null 2>&1
    RC=$?
    if test $RC = 0 ; then
        break
    fi
    echo "Waiting $SLEEP1 seconds for slapd to start..."
    sleep $SLEEP1
done
if test $RC != 0 ; then
    echo "ldapsearch failed ($RC)!"
    test $KILLSERVERS != no && kill -HUP $KILLPIDS
    exit $RC
fi

echo "Running slapadd to build slapd database..."
. $CONFFILTER $BACKEND < $CONFTWO > $CONF3
$SLAPADD -f $CONF3 -l $LDIFORDERED
RC=$?
if test $RC != 0 ; then
    echo "slapadd failed ($RC)!"
    exit $RC
fi

echo "Running slapindex to index slapd database..."
$SLAPINDEX -f $CONF3
RC=$?
if test $RC != 0 ; then
    echo "warning: slapindex failed ($RC)"
    echo "  assuming no indexing support"
fi

echo "Starting second slapd on TCP/IP port $PORT3..."
$SLAPD -f $CONF3 -h $URI3 -d $LVL > $LOG3 2>&1 &
PID=$!
if test $WAIT != 0 ; then
    echo PID $PID
    read foo
fi
PID3="$PID"
KILLPIDS="$KILLPIDS $PID"

sleep $SLEEP0

echo "Testing slapd searching..."
for i in 0 1 2 3 4 5; do
    $LDAPSEARCH -s base -b "$MONITOR" -H $URI3 \
        '(objectclass=*)' > /dev/null 2>&1
    RC=$?
    if test $RC = 0 ; then
        break
    fi
    echo "Waiting $SLEEP1 seconds for slapd to start..."
    sleep $SLEEP1
done
if test $RC != 0 ; then
    echo "ldapsearch failed ($RC)!"
    test $KILLSERVERS != no && kill -HUP $KILLPIDS
    exit $RC
fi

echo "Starting lloadd on TCP/IP port $PORT1..."
. $CONFFILTER $BACKEND < $LLOADDUNREACHABLECONF > $CONF1.lloadd
. $CONFFILTER $BACKEND < $SLAPDLLOADCONF > $CONF1.slapd
$SLAPD -f $CONF1.slapd -h $URI6 -d $LVL > $LOG1 2>&1 &
PID=$!
if test $WAIT != 0 ; then
    echo PID $PID
    read foo
fi
KILLPIDS="$KILLPIDS $PID"

echo "Testing lloadd searching..."
for i in 0 1 2 3 4 5; do
    $LDAPSEARCH -s base -b "$MONITOR" -H $URI1 \
        '(objectclass=*)' > /dev/null 2>&1
    RC=$?
    if test $RC = 0 ; then
        break
    fi
    echo "Waiting $SLEEP1 seconds for lloadd to start..."
    sleep $SLEEP1
done

if test $RC != 0 ; then
    echo "ldapsearch failed ($RC)!"
    test $KILLSERVERS != no && kill -HUP $KILLPIDS
    exit $RC
fi


echo "Testing cn=config searching..."
$LDAPSEARCH -H $URI6 -D cn=config -y $CONFIGPWF \
        -s sub -b "olcBackend={0}lload,cn=config" '(objectclass=*)'  > /dev/null 2>&1
RC=$?
if test $RC != 0 ; then
    echo "ldapsearch failed ($RC)!"
    test $KILLSERVERS != no && kill -HUP $KILLPIDS
    exit $RC
fi

echo "Deleting backends"

# weighted backends are sorted before we get to _cfadd to create their entries
for i in 3 2 4 1 ; do
    echo "cn={0}server $i,cn={1}tier 2,olcBackend={0}lload,cn=config"
    $LDAPDELETE -H $URI6 -D cn=config -y $CONFIGPWF \
    "cn={0}server $i,cn={1}tier 2,olcBackend={0}lload,cn=config" > /dev/null 2>&1
    RC=$?
    if test $RC != 0 ; then
           echo "deleting server failed ($RC)!"
           test $KILLSERVERS != no && kill -HUP $KILLPIDS
           exit $RC
    fi
done

echo "Testing cn=config searching..."
$LDAPSEARCH -H $URI6 -D cn=config -y $CONFIGPWF \
        -s sub -b "olcBackend={0}lload,cn=config" '(objectclass=*)' > /dev/null 2>&1

echo "Deleting tiers"

for i in 1 2; do
    echo "cn={0}tier $i,olcBackend={0}lload,cn=config"
    $LDAPDELETE -H $URI6 -D cn=config -y $CONFIGPWF \
    "cn={0}tier $i,olcBackend={0}lload,cn=config" > /dev/null 2>&1
    RC=$?
    if test $RC != 0 ; then
           echo "deleting server failed ($RC)!"
           test $KILLSERVERS != no && kill -HUP $KILLPIDS
           exit $RC
    fi
done

echo "Testing cn=config searching..."
$LDAPSEARCH -H $URI6 -D cn=config -y $CONFIGPWF \
        -s sub -b "olcBackend={0}lload,cn=config" '(objectclass=*)' > /dev/null 2>&1


echo "# Testing exact searching..."

$LDAPSEARCH -S "" -b "$BASEDN" -H $URI1 \
    '(sn=jENSEN)' >> $SEARCHOUT 2>&1
RC=$?
if test $RC != 52 ; then
    echo "ldapsearch failed ($RC)!"
    test $KILLSERVERS != no && kill -HUP $KILLPIDS
    exit $RC
fi

echo "Testing adding a tier"
$LDAPADD -D cn=config -H $URI6 -y $CONFIGPWF <<EOF > $TESTOUT 2>&1
dn: cn=roundrobin tier,olcBackend={0}lload,cn=config
objectClass: olcBkLloadTierConfig
olcBkLloadTierType: roundrobin
EOF
RC=$?
if test $RC != 0 ; then
    echo "ldapadd failed for cn=roundrobin tier ($RC)!"
    test $KILLSERVERS != no && kill -HUP $KILLPIDS
    exit $RC
fi

echo "Testing adding a weighted tier"
$LDAPADD -D cn=config -H $URI6 -y $CONFIGPWF <<EOF > $TESTOUT 2>&1
dn: cn=weighted tier,olcBackend={0}lload,cn=config
objectClass: olcBkLloadTierConfig
olcBkLloadTierType: weighted
EOF
RC=$?
if test $RC != 0 ; then
    echo "ldapadd failed for cn=weighted tier ($RC)!"
    test $KILLSERVERS != no && kill -HUP $KILLPIDS
    exit $RC
fi

echo "Testing adding a backend server"
$LDAPADD -D cn=config -H $URI6 -y $CONFIGPWF <<EOF > $TESTOUT 2>&1
dn: cn=server 7,cn={1}weighted tier,olcBackend={0}lload,cn=config
objectClass: olcBkLloadBackendConfig
cn: server 7
olcBkLloadBackendUri: $URI3
olcBkLloadBindconns: 2
olcBkLloadMaxPendingConns: 3
olcBkLloadMaxPendingOps: 5
olcBkLloadNumconns: 3
olcBkLloadRetry: 5000
olcBkLloadWeight: 10
EOF
RC=$?
if test $RC != 0 ; then
    echo "ldapadd failed for cn=server 7 ($RC)!"
    test $KILLSERVERS != no && kill -HUP $KILLPIDS
    exit $RC
fi

echo "Verifying balancer operation..."
for i in 0 1 2 3 4 5; do
    $LDAPSEARCH -S "" -b "$BASEDN" -H $URI1 \
    '(sn=jENSEN)' >> $SEARCHOUT 2>&1
    RC=$?
    if test $RC = 0 ; then
        break
    fi
    echo "Waiting $SLEEP1 seconds for backend to start..."
    sleep $SLEEP1
done

echo "Testing bindconf modify"
$LDAPMODIFY -D cn=config -H $URI6 -y $CONFIGPWF <<EOF >> $TESTOUT 2>&1
dn: olcBackend={0}lload,cn=config
changetype: modify
replace: olcBkLloadBindconf
olcBkLloadBindconf: bindmethod=simple timeout=0 network-timeout=0 binddn="cn=wrongmanager,dc=example,dc=com" credentials="secret"
EOF

RC=$?
if test $RC != 0 ; then
    echo "modify failed for bindconf ($RC)!"
    test $KILLSERVERS != no && kill -HUP $KILLPIDS
    exit $RC
fi

echo "# Sending a search request..."

$LDAPSEARCH -S "" -b "$BASEDN" -H $URI1 \
    '(sn=jENSEN)' >> $SEARCHOUT 2>&1
RC=$?
if test $RC != 52 ; then
    echo "ldapsearch failed ($RC)!"
    test $KILLSERVERS != no && kill -HUP $KILLPIDS
    exit $RC
fi

echo "Restoring bindconf value"
$LDAPMODIFY -D cn=config -H $URI6 -y $CONFIGPWF <<EOF >> $TESTOUT 2>&1
dn: olcBackend={0}lload,cn=config
changetype: modify
replace: olcBkLloadBindconf
olcBkLloadBindconf: bindmethod=simple timeout=0 network-timeout=0 binddn="cn=Manager,dc=example,dc=com" credentials="secret"
EOF

RC=$?
if test $RC != 0 ; then
    echo "modify failed for bindconf ($RC)!"
    test $KILLSERVERS != no && kill -HUP $KILLPIDS
    exit $RC
fi


echo "Verifying balancer operation..."
for i in 0 1 2 3 4 5; do
    $LDAPSEARCH -S "" -b "$BASEDN" -H $URI1 \
    '(sn=jENSEN)' >> $SEARCHOUT 2>&1
    RC=$?
    if test $RC = 0 ; then
        break
    fi
    echo "Waiting $SLEEP1 seconds for backend to start..."
    sleep $SLEEP1
done

echo "Testing global attributes"
echo "Testing olcBkLloadMaxPDUPerCycle modify"
$LDAPMODIFY -D cn=config -H $URI6 -y $CONFIGPWF <<EOF >> $TESTOUT 2>&1
dn: olcBackend={0}lload,cn=config
changetype: modify
replace: olcBkLloadMaxPDUPerCycle
olcBkLloadMaxPDUPerCycle: 0
EOF

RC=$?
if test $RC != 0 ; then
    echo "modify failed for olcBkLloadMaxPDUPerCycle($RC)!"
    test $KILLSERVERS != no && kill -HUP $KILLPIDS
    exit $RC
fi

echo "Sending a search request..."
$LDAPSEARCH -S "" -b "$BASEDN" -H $URI1 \
    '(sn=jENSEN)' >> $SEARCHOUT 2>&1
RC=$?
if test $RC != 0 ; then
    echo "ldapsearch failed ($RC)!"
    test $KILLSERVERS != no && kill -HUP $KILLPIDS
    exit $RC
fi

echo "Testing olcBkLloadSockbufMaxClient modify"
$LDAPMODIFY -D cn=config -H $URI6 -y $CONFIGPWF <<EOF >> $TESTOUT 2>&1
dn: olcBackend={0}lload,cn=config
changetype: modify
replace: olcBkLloadSockbufMaxClient
olcBkLloadSockbufMaxClient: 20000
EOF

RC=$?
if test $RC != 0 ; then
    echo "modify failed for olcBkLloadSockbufMaxClient($RC)!"
    test $KILLSERVERS != no && kill -HUP $KILLPIDS
    exit $RC
fi

echo "Sending a search request..."
$LDAPSEARCH -S "" -b "$BASEDN" -H $URI1 \
    '(sn=jENSEN)' >> $SEARCHOUT 2>&1
RC=$?
if test $RC != 0 ; then
    echo "ldapsearch failed ($RC)!"
    test $KILLSERVERS != no && kill -HUP $KILLPIDS
    exit $RC
fi

echo "Testing olcBkLloadSockbufMaxUpstream modify"
$LDAPMODIFY -D cn=config -H $URI6 -y $CONFIGPWF <<EOF >> $TESTOUT 2>&1
dn: olcBackend={0}lload,cn=config
changetype: modify
replace: olcBkLloadSockbufMaxUpstream
olcBkLloadSockbufMaxUpstream: 200000
EOF

RC=$?
if test $RC != 0 ; then
    echo "modify failed for olcBkLloadSockbufMaxUpstream($RC)!"
    test $KILLSERVERS != no && kill -HUP $KILLPIDS
    exit $RC
fi

echo "Sending a search request..."
$LDAPSEARCH -S "" -b "$BASEDN" -H $URI1 \
    '(sn=jENSEN)' >> $SEARCHOUT 2>&1
RC=$?
if test $RC != 0 ; then
    echo "ldapsearch failed ($RC)!"
    test $KILLSERVERS != no && kill -HUP $KILLPIDS
    exit $RC
fi

echo "Testing olcBkLloadIOTimeout modify"
$LDAPMODIFY -D cn=config -H $URI6 -y $CONFIGPWF <<EOF >> $TESTOUT 2>&1
dn: olcBackend={0}lload,cn=config
changetype: modify
replace: olcBkLloadIOTimeout
olcBkLloadIOTimeout: 20000
EOF

RC=$?
if test $RC != 0 ; then
    echo "modify failed for olcBkLloadWriteTimeout($RC)!"
    test $KILLSERVERS != no && kill -HUP $KILLPIDS
    exit $RC
fi

echo "Sending a search request..."
$LDAPSEARCH -S "" -b "$BASEDN" -H $URI1 \
    '(sn=jENSEN)' >> $SEARCHOUT 2>&1
RC=$?
if test $RC != 0 ; then
    echo "ldapsearch failed ($RC)!"
    test $KILLSERVERS != no && kill -HUP $KILLPIDS
    exit $RC
fi

echo "Testing backend attributes"
echo "Testing olcBkLloadBindconns modify"
$LDAPMODIFY -D cn=config -H $URI6 -y $CONFIGPWF <<EOF >> $TESTOUT 2>&1
dn: cn={0}server 7,cn={1}weighted tier,olcBackend={0}lload,cn=config
changetype: modify
replace: olcBkLloadBindconns
olcBkLloadBindconns: 20
EOF

RC=$?
if test $RC != 0 ; then
    echo "modify failed for olcBkLloadBindconns($RC)!"
    test $KILLSERVERS != no && kill -HUP $KILLPIDS
    exit $RC
fi

echo "Testing exact searching..."
$LDAPSEARCH -S "" -b "$BASEDN" -H $URI1 \
    '(sn=jENSEN)' >> $SEARCHOUT 2>&1
RC=$?
if test $RC != 0 ; then
    echo "ldapsearch failed ($RC)!"
    test $KILLSERVERS != no && kill -HUP $KILLPIDS
    exit $RC
fi


echo "Testing olcBkLloadMaxPendingConns modify"
$LDAPMODIFY -D cn=config -H $URI6 -y $CONFIGPWF <<EOF >> $TESTOUT 2>&1
dn: cn={0}server 7,cn={1}weighted tier,olcBackend={0}lload,cn=config
changetype: modify
replace: olcBkLloadMaxPendingConns
olcBkLloadMaxPendingConns: 30
EOF

RC=$?
if test $RC != 0 ; then
    echo "modify failed for olcBkLloadMaxPendingConns($RC)!"
    test $KILLSERVERS != no && kill -HUP $KILLPIDS
    exit $RC
fi

echo "Testing exact searching..."
$LDAPSEARCH -S "" -b "$BASEDN" -H $URI1 \
    '(sn=jENSEN)' >> $SEARCHOUT 2>&1
RC=$?
if test $RC != 0 ; then
    echo "ldapsearch failed ($RC)!"
    test $KILLSERVERS != no && kill -HUP $KILLPIDS
    exit $RC
fi

test $KILLSERVERS != no && kill -HUP $KILLPIDS


echo ">>>>> Test succeeded"

test $KILLSERVERS != no && wait

exit 0
